package com.demo.gereric;

import java.util.concurrent.Callable;

public class Base<R> {
	private Callable<R> callable;

	private R a;

	public Base<R> callable(final Callable<R> callable) {
		this.callable = callable;
		return this;
	}

	public static <R> Base<R> newInstance() {
		return new Base<>();
	}

	public R retryCall() {
		// 初始化
		RetryContext<R> context = context();
		R execute = context.execute(context);
		return execute;
	}

	private RetryContext<R> context() {
		RetryContext context = new RetryContext();
		context.setCallable(this.callable);
		return context;
	}


	public void testMethod(R r) {
		System.out.println(r.getClass().getName());
	}

	public <R> R testMethod1(R r) {
		return r;
	}

	public static void main(String[] args) {
		Base<String> t = new Base();
		t.testMethod("generic");
		Integer integer = 1;
		Integer i = t.testMethod1(integer);
	}

}

class RetryContext<R> {
	/**
	 * 可执行的方法
	 */
	private Callable<R> callable;

	public RetryContext<R> setCallable(final Callable<R> callable) {
		this.callable = callable;
		return this;
	}

	public R execute(final RetryContext<R> context) {
		R result = null;
		try {
			result = callable.call();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
}